package com.wowangz.cms.syscore.autotask.job;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.autotask.pojo.MQQueueData;
import com.wowangz.cms.syscore.autotask.service.StoreDatabaseResourceService;
import com.wowangz.cms.syscore.pojo.RouteInfo;
import com.wowangz.cms.syscore.utils.DataPageUtil;
import com.wowangz.cms.systemmanage.email.set.entity.SystemEmail;
import com.wowangz.cms.systemmanage.email.set.service.SystemEmailService;
import com.wowangz.cms.systemmanage.sms.smsset.entity.SmsSet;
import com.wowangz.cms.systemmanage.sms.smsset.service.SmsSetService;

public class AgreementNoticeJob extends BaseJob {

	/**
	 * 日志对象
	 */
	private static final Logger logger = LoggerFactory.getLogger(AgreementNoticeJob.class);

	@Override
	public void innerExecute(JobExecutionContext context) throws Exception {

		logger.info("协议提醒自动任务开始运行");
		try {
			HashMap<String, Object> query = new HashMap<String, Object>();
			query.put("state", SysConstant.CommonApproveStatus.OK);
			query.put("statusid", SysConstant.RecordStatus.VALID);
			StoreDatabaseResourceService storeDatabaseResourceService = (StoreDatabaseResourceService) IssTransactionContainer
					.getInstance(StoreDatabaseResourceService.class, false);
			long count = storeDatabaseResourceService.queryStoreDatabaseResourceCount(query);
			int pageStartRow = 0, pageEndRow = 0;
			int page = DataPageUtil.totalPages(Long.valueOf(count).intValue());
			for (int i = 1; i < page + 1; i++) {
				pageStartRow = DataPageUtil.pageNum * (i - 1);
				pageEndRow = DataPageUtil.pageNum;
				query.put("startrow", pageStartRow);
				query.put("endrow", pageEndRow);
				List<RouteInfo> list = storeDatabaseResourceService.queryStoreDatabaseResource(query);
				MQQueueData data = null;
				Gson json = new Gson();
				for (RouteInfo routeInfo : list) {
					data = new MQQueueData();
					data.setStoreid(routeInfo.getStoreid());
					data.setDbip(routeInfo.getIp());
					data.setDbname(routeInfo.getDbname());
					data.setDbpassword(routeInfo.getPassword());
					data.setDbusername(routeInfo.getUsername());
					data.setDbport(routeInfo.getPort());
					// 短信模板
					SmsSetService smsSetService = (SmsSetService) IssTransactionContainer.getInstance(SmsSetService.class, false);
					Map<String, Object> paramsMap = new HashMap<String, Object>();
					paramsMap.put("templatetype", SysConstant.Templatetype.AGREEMENTMANAGE);
					paramsMap.put("used", SysConstant.CommonUsedStatus.START);
					paramsMap.put("statusid", SysConstant.RecordStatus.VALID);
					SmsSet smsSet = smsSetService.querySmsSetByMap(paramsMap);
					data.setSmsSet(smsSet);
					// 邮件模板
					SystemEmailService emailService = (SystemEmailService) IssTransactionContainer.getInstance(SystemEmailService.class, false);
					paramsMap.put("templatetype", SysConstant.TemplateEmailType.AGREEMENTMANAGE);
					SystemEmail systemEmail = emailService.queryEmailSetByMap(paramsMap);
					data.setSystemEmail(systemEmail);
					this.sendTransDataToMq(json.toJson(data), SysConstant.MQQUEUE.MQ_AGREEMENTMANAGE);
				}
			}
			logger.info("协议提醒自动任务结束运行");
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}
}
